package com.car.service.impl;

import com.car.exception.BusinessException;
import com.car.mapper.JobMapper;
import com.car.pojo.Job;
import com.car.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 职位管理服务实现类
 */
@Service
public class JobServiceImpl implements JobService {

    @Autowired
    private JobMapper jobMapper;

    @Override
    public List<Job> findAll() {
        return jobMapper.findAll();
    }

    @Override
    public void deleteById(Integer id) {
        // 判断职位下是否有员工，如果有，抛出异常
        Integer count = jobMapper.countByJobId(id);
        if(count > 0){
            throw new BusinessException("职位下有员工，不能删除");
        }

        // 删除职位
        jobMapper.deleteById(id);
    }

    @Override
    public void add(Job job) {
        // 补全基础属性 - createTime, updateTime
        job.setCreateTime(LocalDateTime.now());
        job.setUpdateTime(LocalDateTime.now());

        // 调用Mapper接口方法插入数据
        jobMapper.insert(job);
    }

    @Override
    public Job getById(Integer id) {
        return jobMapper.getById(id);
    }

    @Override
    public void update(Job job) {
        // 补全基础属性-updateTime
        job.setUpdateTime(LocalDateTime.now());

        // 调用Mapper接口方法更新职位
        jobMapper.update(job);
    }
}